﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using TmU220PrintHelperV2d0.ControlUi;
using TmU220PrintHelperV2d0.Entity;
using TmU220PrintHelperV2d0.Singleton;
using ZhangLogSysV1d0;
/// <summary>
/// 打印任务项,一个打印任务包含多条打印任务项
/// zhangfujian-2019-1
/// </summary>
namespace TmU220PrintHelperV2d0.Entity
{
    /// <summary>
    /// 打印任务项，一个打印任务包含多条打印任务项
    /// </summary>
    [XmlRoot("打印任务项")]
    [Serializable]
    public class PrintTaskItemV2d0
    {
        [XmlAttribute("任务项标识")]
        public Guid Id { get; set; }
        /// <summary>
        /// 打印顺序号
        /// </summary>
        [XmlAttribute("打印顺序号")]
        public int PrintSequence { get; set; }
        /// <summary>
        /// 格式名称
        /// </summary>
        [XmlElement("内容格式")]
        public PrintContentFormatV2d0 FormatOfContent { get; set; }
        /// <summary>
        /// 内容文本
        /// </summary>
        [XmlAttribute("内容")]
        private string _content;
        public string Content
        {
            get
            {
                return _content;
            }
            set
            {
                //自动在末尾加回车符
                if (value.Length > 0 && value.Last() != '\n') _content = value + "\n";
                else _content = value;
            }
        }
        /// <summary>
        /// 绘制有打印内容的位图，用于图像打印模式
        /// </summary>
        [XmlIgnore]
        public Bitmap BitmapOfContent { get; set; }
        /// <summary>
        /// 打印内容的区域类型，表单头Header，表单明细Detail，表单脚Footer
        /// </summary>
        [XmlElement(ElementName = "区域类型", Type = typeof(AreaTypeV2d0))]
        public AreaTypeV2d0 AreaType { get; set; }
        public PrintTaskItemV2d0()
        {
            Id = Guid.NewGuid();
            PrintSequence = 0;
            FormatOfContent = new PrintContentFormatV2d0();
            Content = "";
            BitmapOfContent = new Bitmap(400, 32);
        }
        public int GetValidPrintAreaWidth(string deviceType)
        {
            if (deviceType == "TM-U220") return 396;
            else if (deviceType == "XP-Q200") return 570;
            else return 396;
        }
        /// <summary>
        /// 核心方法
        /// 用模板行构建打印任务项
        /// </summary>
        /// <param name="templetRow"></param>
        public PrintTaskItemV2d0(PrintTempletRowV2d0 printTempletRow,string deviceType="TM-U220")
        {
            Id = Guid.NewGuid();
            int validPrintAreaWidth = GetValidPrintAreaWidth(deviceType);
            #region 文本打印模式相关
            //内容格式和区域类型
            FormatOfContent = new PrintContentFormatV2d0(printTempletRow);
            AreaType = printTempletRow.AreaType;
            //处理左对齐元素
            List<PrintTempletElementV2d0> leftElementList = printTempletRow.TempletElementList.FindAll(x => x.HorizontalAlign == "Left");
            string contentLeft = string.Empty;
            if (leftElementList.Count > 0)
            {
                foreach (var item in leftElementList)
                {
                    contentLeft = contentLeft + item.ContentText;
                }
            }

            //处理右对齐元素
            List<PrintTempletElementV2d0> rightElementList = printTempletRow.TempletElementList.FindAll(x => x.HorizontalAlign == "Right");
            string contentRight = string.Empty;
            if (rightElementList.Count > 0)
            {
                foreach (var item in rightElementList)
                {
                    contentRight = contentRight + item.ContentText;
                }
            }
            //处理居中对齐元素
            List<PrintTempletElementV2d0> centerElementList = printTempletRow.TempletElementList.FindAll(x => x.HorizontalAlign == "Center");
            string contentCenter = string.Empty;
            if (centerElementList.Count > 0)
            {
                foreach (var item in centerElementList)
                {
                    contentCenter = contentCenter + item.ContentText;
                }
            }
            if (string.IsNullOrEmpty(contentCenter))
            {
                Content = PrintTaskV2d0.GetSingleLineStringLeftRight(contentLeft, contentRight, printTempletRow.TempletElementList[0].IsDoubleWidth, validPrintAreaWidth);
            }
            else
            {
                Content = PrintTaskV2d0.GetSingleLineStringLeftCenterRight(contentLeft, contentCenter, contentRight, printTempletRow.TempletElementList[0].IsDoubleWidth, validPrintAreaWidth);
            }
            //XP-Q200打印机一行可以打印48个asc字符
            if(Content.IndexOf("----------------------------------------")>=0&& deviceType=="XP-Q200")
            {
                Content = new string('-', 48);
            }
            #endregion
            #region 图像打印模式相关
            //生成用于图像打印模式的图像
            //取字体最大的模板行的元素来确定图像的高度
            int canvasHeight = PrintTempletRowDisplay.GetSuitableHeight(printTempletRow,deviceType);
            int canvasWidth = 400;//固定
            canvasWidth = validPrintAreaWidth;
            BitmapOfContent = new Bitmap(canvasWidth, canvasHeight);
            Graphics canvas = Graphics.FromImage(BitmapOfContent);
            //依次在画板上绘制元素
            PrintTempletRowDisplay.DrawHeaderFooterRowOnGraphics(printTempletRow, canvas, canvasWidth, canvasHeight, deviceType);
            #endregion
        } 
       
    }
}
